﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template name="DeleteText">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template name="PasteComment">
    <xsl:param name="Region"/>
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:if test="$Region='true'">
        <xsl:text>Количество внешних контуров: </xsl:text>
        <xsl:value-of select="count(../*/MIF/DATA/region)"/>
        <xsl:if test="count(../*/MIF/DATA/region)>0">
          <xsl:text>. Общая площадь: </xsl:text>
          <xsl:value-of select="sum(../*/MIF/DATA/region/@Square)"/>
        </xsl:if>
        <xsl:text>.</xsl:text>
      </xsl:if>
      <xsl:if test="$Region='false'">
        <xsl:text>Количество фрагментов границы: </xsl:text>
        <xsl:value-of select="count(../*/MIF/DATA/pline)"/>
        <xsl:text>.</xsl:text>
      </xsl:if>
    </xsl:copy>
  </xsl:template>

  <xsl:template name="PastePoints">
    <xsl:param name="XY0"/>
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:for-each select="../*/*/*/MIF/DATA/region">
        <xsl:variable name="Region" select="position()"/>
        <xsl:variable name="Polygons0" select="count(polygon)"/>
        <xsl:for-each select="polygon">
          <xsl:variable name="Polygon" select="position()"/>
          <xsl:for-each select="point[last()>position()]">
            <xsl:variable name="Point" select="position()"/>
            <xsl:variable name="RegionPoints" select="count(../../../region[$Region]/preceding-sibling::region/polygon/point)"/>
            <xsl:variable name="PolygonPoints" select="count(../../../region[$Region]/polygon[$Polygon]/preceding-sibling::polygon/point)"/>
            <xsl:variable name="RegionPolygons" select="count(../../../region[$Region]/preceding-sibling::region/polygon)"/>
            <xsl:variable name="Points" select="$RegionPoints+$PolygonPoints+$Point"/>
            <xsl:variable name="Polygons" select="$RegionPolygons+$Polygon"/>
            <xsl:variable name="X" select="X1"/>
            <xsl:variable name="Y" select="Y1"/>
            <Section>
              <xsl:attribute name="SetNumber">
                <xsl:value-of select="$Points - $Polygons + 1"/>
              </xsl:attribute>
              <xsl:attribute name="SetID">
                <xsl:value-of select="$Points - $Polygons + 1"/>
              </xsl:attribute>
              <xsl:for-each select="../../../../../../../../Multi[@Code='Point']/Section[1]/*">
                <xsl:copy>
                  <xsl:choose>
                    <xsl:when test="@Code='Num_Geopoint'">
                      <xsl:apply-templates select="@*"/>
                      <xsl:value-of select="$Points - $Polygons + 1"/>
                      <xsl:if test="$Region > 1 or $Polygons0 > 1" >
                        <xsl:text> (</xsl:text>
                        <xsl:value-of select="$Region"/>
                        <xsl:if test="$Polygon > 1">
                          <xsl:text>.</xsl:text>
                          <xsl:value-of select="$Polygon - 1"/>
                        </xsl:if>
                        <xsl:text>)</xsl:text>
                      </xsl:if>
                    </xsl:when>
                    <xsl:when test="@Code='X'">
                      <xsl:apply-templates select="@*"/>
                      <xsl:value-of select="$X"/>
                    </xsl:when>
                    <xsl:when test="@Code='Y'">
                      <xsl:apply-templates select="@*"/>
                      <xsl:value-of select="$Y"/>
                    </xsl:when>
                    <xsl:when test="@Code='X0' and $XY0='true'">
                      <xsl:apply-templates select="@*"/>
                      <xsl:value-of select="$X"/>
                    </xsl:when>
                    <xsl:when test="@Code='Y0' and $XY0='true'">
                      <xsl:apply-templates select="@*"/>
                      <xsl:value-of select="$Y"/>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:apply-templates select="@* | node()"/>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:copy>
              </xsl:for-each>
            </Section>
          </xsl:for-each>
        </xsl:for-each>
      </xsl:for-each>
      <xsl:if test="count(../*/*/*/MIF/DATA/region)=0">
        <Section>
          <xsl:attribute name="SetNumber">
            <xsl:value-of select="1"/>
          </xsl:attribute>
          <xsl:attribute name="SetID">
            <xsl:value-of select="1"/>
          </xsl:attribute>
          <xsl:for-each select="Section[1]/*">
            <xsl:copy>
              <xsl:choose>
                <xsl:when test="@Code='Point_Pref'">
                  <xsl:apply-templates select="@*"/>
                </xsl:when>
                <xsl:when test="@Code='Num_Geopoint'">
                  <xsl:apply-templates select="@*"/>                  
                </xsl:when>
                <xsl:when test="@Code='X'">
                  <xsl:apply-templates select="@*"/>                  
                </xsl:when>
                <xsl:when test="@Code='Y'">
                  <xsl:apply-templates select="@*"/>                  
                </xsl:when>
                <xsl:when test="@Code='X0'">
                  <xsl:apply-templates select="@*"/>                  
                </xsl:when>
                <xsl:when test="@Code='Y0'">
                  <xsl:apply-templates select="@*"/>                  
                </xsl:when>
                <xsl:when test="@Code='Delta_Geopoint'">
                  <xsl:apply-templates select="@*"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:apply-templates select="@* | node()"/>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:copy>
          </xsl:for-each>
        </Section>
      </xsl:if>
    </xsl:copy>
  </xsl:template>

  <xsl:template name="PasteEdges">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:for-each select="../*/*/*/MIF/DATA/region">
        <xsl:variable name="Region" select="position()"/>
        <xsl:variable name="Polygons0" select="count(polygon)"/>
        <xsl:for-each select="polygon">
          <xsl:variable name="Polygon" select="position()"/>
          <xsl:for-each select="point[position()>1]">
            <xsl:variable name="Point" select="position()"/>
            <xsl:variable name="CountPoint" select="count(../point)"/>
            <xsl:variable name="RegionPoints" select="count(../../../region[$Region]/preceding-sibling::region/polygon/point)"/>
            <xsl:variable name="PolygonPoints" select="count(../../../region[$Region]/polygon[$Polygon]/preceding-sibling::polygon/point)"/>
            <xsl:variable name="RegionPolygons" select="count(../../../region[$Region]/preceding-sibling::region/polygon)"/>
            <xsl:variable name="Points" select="$RegionPoints+$PolygonPoints+$Point"/>
            <xsl:variable name="Polygons" select="$RegionPolygons+$Polygon"/>
            <xsl:variable name="Length" select="Length"/>
            <Section>
              <xsl:attribute name="SetNumber">
                <xsl:value-of select="$Points - $Polygons + 1"/>
              </xsl:attribute>
              <xsl:attribute name="SetID">
                <xsl:value-of select="$Points - $Polygons + 1"/>
              </xsl:attribute>
              <xsl:for-each select="../../../../../../../../Multi[@Code='Edge']/Section[1]/*">
                <xsl:copy>
                  <xsl:choose>
                    <xsl:when test="@Code='FromTo'">
                      <xsl:apply-templates select="@*"/>
                      <xsl:value-of select="$Points - $Polygons + 1"/>
                      <xsl:text>-</xsl:text>
                      <xsl:choose>
                        <xsl:when test="$Point = $CountPoint - 1">
                          <xsl:value-of select="$Points - $Polygons + 2 - $Point"/>
                        </xsl:when>
                        <xsl:otherwise>
                          <xsl:value-of select="$Points - $Polygons + 2"/>
                        </xsl:otherwise>
                      </xsl:choose>
                      <xsl:if test="$Region > 1 or $Polygons0 > 1" >
                        <xsl:text> (</xsl:text>
                        <xsl:value-of select="$Region"/>
                        <xsl:if test="$Polygon > 1">
                          <xsl:text>.</xsl:text>
                          <xsl:value-of select="$Polygon - 1"/>
                        </xsl:if>
                        <xsl:text>)</xsl:text>
                      </xsl:if>
                    </xsl:when>
                    <xsl:when test="@Code='Length'">
                      <xsl:apply-templates select="@*"/>
                      <xsl:value-of select="$Length"/>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:apply-templates select="@* | node()"/>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:copy>
              </xsl:for-each>
            </Section>
          </xsl:for-each>
        </xsl:for-each>
      </xsl:for-each>
      <xsl:if test="count(../*/*/*/MIF/DATA/region)=0">
        <Section>
          <xsl:attribute name="SetNumber">
            <xsl:value-of select="1"/>
          </xsl:attribute>
          <xsl:attribute name="SetID">
            <xsl:value-of select="1"/>
          </xsl:attribute>
          <xsl:for-each select="Section[1]/*">
            <xsl:copy>
              <xsl:choose>
                <xsl:when test="@Code='FromTo'">
                  <xsl:apply-templates select="@*"/>
                </xsl:when>
                <xsl:when test="@Code='Length'">
                  <xsl:apply-templates select="@*"/>
                </xsl:when>
                <xsl:when test="@Code='Definition'">
                  <xsl:apply-templates select="@*"/>
                </xsl:when>                
                <xsl:otherwise>
                  <xsl:apply-templates select="@* | node()"/>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:copy>
          </xsl:for-each>
        </Section>
      </xsl:if>   
    </xsl:copy>
  </xsl:template>

  <xsl:template name="PasteSquares">
    <xsl:param name="Number_PP"/>
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:for-each select="../*/*/*/MIF/DATA/region">
        <xsl:variable name="Region" select="position()"/>
        <xsl:variable name="Square" select="@Square"/>
        <xsl:variable name="Formula1">
          <xsl:text>S(</xsl:text>
          <xsl:value-of select="$Region"/>
          <xsl:text>)</xsl:text>
          <xsl:for-each select="polygon[position()>1]">
            <xsl:text>-S(</xsl:text>
            <xsl:value-of select="$Region"/>
            <xsl:text>.</xsl:text>
            <xsl:value-of select="position()"/>
            <xsl:text>)</xsl:text>
          </xsl:for-each>
        </xsl:variable>
        <xsl:variable name="Formula2">
          <xsl:for-each select="polygon">
            <xsl:value-of select="@Square"/>
          </xsl:for-each>
        </xsl:variable>
        <Section>
          <xsl:attribute name="SetNumber">
            <xsl:value-of select="$Region"/>
          </xsl:attribute>
          <xsl:attribute name="SetID">
            <xsl:value-of select="$Region"/>
          </xsl:attribute>
          <xsl:for-each select="../../../../../../Multi[@Code='Square']/Section[1]/*">
            <xsl:copy>
              <xsl:choose>
                <xsl:when test="@Code='Contour'">
                  <xsl:apply-templates select="@*"/>
                  <xsl:text>(</xsl:text>
                  <xsl:value-of select="$Region"/>
                  <xsl:text>)</xsl:text>
                </xsl:when>
                <xsl:when test="@Code='Number_PP' and $Number_PP='true'">
                  <xsl:apply-templates select="@*"/>
                  <xsl:value-of select="$Region"/>
                </xsl:when>
                <xsl:when test="@Code='Area'">
                  <xsl:apply-templates select="@*"/>
                  <xsl:value-of select="$Square"/>
                </xsl:when>
                <xsl:when test="@Code='Formula'">
                  <xsl:apply-templates select="@*"/>
                  <xsl:value-of select="$Formula1"/>
                  <xsl:text> = </xsl:text>
                  <xsl:value-of select="$Formula2"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:apply-templates select="@* | node()"/>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:copy>
          </xsl:for-each>
        </Section>

      </xsl:for-each>
      <xsl:if test="count(../*/*/*/MIF/DATA/region)=0">
        <Section>
          <xsl:attribute name="SetNumber">
            <xsl:value-of select="1"/>
          </xsl:attribute>
          <xsl:attribute name="SetID">
            <xsl:value-of select="1"/>
          </xsl:attribute>
          <xsl:for-each select="Section[1]/*">
            <xsl:copy>
              <xsl:choose>
                <xsl:when test="@Code='Contour'">
                  <xsl:apply-templates select="@*"/>
                  <xsl:value-of select="'(1)'"/>
                </xsl:when>
                <xsl:when test="@Code='Number_PP'">
                  <xsl:apply-templates select="@*"/>
                  <xsl:value-of select="'1'"/>
                </xsl:when>
                <xsl:when test="@Code='Area'">
                  <xsl:apply-templates select="@*"/>
                  <xsl:value-of select="'0'"/>
                </xsl:when>
                <xsl:when test="@Code='Formula'">
                  <xsl:apply-templates select="@*"/>
                  <xsl:value-of select="'S(1) = 0'"/>
                </xsl:when>
                <xsl:when test="@Code='Definition'">
                  <xsl:apply-templates select="@*"/>
                  <xsl:value-of select="'-'"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:apply-templates select="@* | node()"/>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:copy>
          </xsl:for-each>
        </Section>
      </xsl:if>
    </xsl:copy>
  </xsl:template>

  <xsl:template name="PastePointsP2">
    <xsl:param name="XY0"/>
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:for-each select="../*/*/*/MIF/DATA/pline">
        <xsl:variable name="Region" select="position()"/>
        <xsl:for-each select="line">
          <xsl:variable name="Polygon" select="position()"/>
          <xsl:for-each select="point">
            <xsl:variable name="Point" select="position()"/>
            <xsl:variable name="RegionPoints" select="count(../../../pline[$Region]/preceding-sibling::pline/line/point)"/>
            <xsl:variable name="PolygonPoints" select="count(../../../pline[$Region]/line[$Polygon]/preceding-sibling::line/point)"/>
            <xsl:variable name="RegionPolygons" select="count(../../../pline[$Region]/preceding-sibling::pline/line)"/>
            <xsl:variable name="Points" select="$RegionPoints+$PolygonPoints+$Point"/>
            <xsl:variable name="Polygons" select="$RegionPolygons+$Polygon"/>
            <xsl:variable name="X" select="X1"/>
            <xsl:variable name="Y" select="Y1"/>
            <Section>
              <xsl:attribute name="SetNumber">
                <xsl:value-of select="$Polygon"/>
                <xsl:text>_</xsl:text>
                <xsl:value-of select="$Point"/>
              </xsl:attribute>
              <xsl:attribute name="SetID">
                <xsl:value-of select="$Polygon"/>
                <xsl:text>_</xsl:text>
                <xsl:value-of select="$Point"/>
              </xsl:attribute>
              <xsl:for-each select="../../../../../../../../Multi[@Code='Point']/Section[1]/*">
                <xsl:copy>
                  <xsl:choose>
                    <xsl:when test="@Code='Num_Geopoint'">
                      <xsl:apply-templates select="@*"/>
                      <xsl:value-of select="$Points - $Polygons +1"/>
                      <xsl:if test="$Polygons > 1" >
                        <xsl:text> (</xsl:text>
                        <xsl:value-of select="$Polygons"/>
                        <xsl:text>)</xsl:text>
                      </xsl:if>
                    </xsl:when>
                    <xsl:when test="@Code='X0' and $XY0='true'">
                      <xsl:apply-templates select="@*"/>
                      <xsl:value-of select="$X"/>
                    </xsl:when>
                    <xsl:when test="@Code='Y0' and $XY0='true'">
                      <xsl:apply-templates select="@*"/>
                      <xsl:value-of select="$Y"/>
                    </xsl:when>
                    <xsl:when test="@Code='X'">
                      <xsl:apply-templates select="@*"/>
                      <xsl:value-of select="$X"/>
                    </xsl:when>
                    <xsl:when test="@Code='Y'">
                      <xsl:apply-templates select="@*"/>
                      <xsl:value-of select="$Y"/>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:apply-templates select="@* | node()"/>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:copy>
              </xsl:for-each>
            </Section>
          </xsl:for-each>
        </xsl:for-each>
      </xsl:for-each>
      <xsl:if test="count(../*/*/*/MIF/DATA/pline)=0">
        <Section>
          <xsl:attribute name="SetNumber">
            <xsl:value-of select="1"/>
          </xsl:attribute>
          <xsl:attribute name="SetID">
            <xsl:value-of select="1"/>
          </xsl:attribute>
          <xsl:for-each select="Section[1]/*">
            <xsl:copy>
              <xsl:choose>
                <xsl:when test="@Code='Point_Pref'">
                  <xsl:apply-templates select="@*"/>
                </xsl:when>
                <xsl:when test="@Code='Num_Geopoint'">
                  <xsl:apply-templates select="@*"/>
                </xsl:when>
                <xsl:when test="@Code='X'">
                  <xsl:apply-templates select="@*"/>
                </xsl:when>
                <xsl:when test="@Code='Y'">
                  <xsl:apply-templates select="@*"/>
                </xsl:when>
                <xsl:when test="@Code='X0'">
                  <xsl:apply-templates select="@*"/>
                </xsl:when>
                <xsl:when test="@Code='Y0'">
                  <xsl:apply-templates select="@*"/>
                </xsl:when>
                <xsl:when test="@Code='Delta_Geopoint'">
                  <xsl:apply-templates select="@*"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:apply-templates select="@* | node()"/>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:copy>
          </xsl:for-each>
        </Section>
      </xsl:if>
    </xsl:copy>
  </xsl:template>

  <xsl:template name="PasteEdgesP2">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:for-each select="../*/*/*/MIF/DATA/pline">
        <xsl:variable name="Region" select="position()"/>
        <xsl:for-each select="line">
          <xsl:variable name="Polygon" select="position()"/>
          <xsl:for-each select="point[position()>1]">
            <xsl:variable name="Point" select="position()"/>
            <xsl:variable name="CountPoint" select="count(../point)"/>
            <xsl:variable name="RegionPoints" select="count(../../../pline[$Region]/preceding-sibling::pline/line/point)"/>
            <xsl:variable name="PolygonPoints" select="count(../../../pline[$Region]/line[$Polygon]/preceding-sibling::line/point)"/>
            <xsl:variable name="RegionPolygons" select="count(../../../pline[$Region]/preceding-sibling::pline/line)"/>
            <xsl:variable name="Points" select="$RegionPoints+$PolygonPoints+$Point"/>
            <xsl:variable name="Polygons" select="$RegionPolygons+$Polygon"/>
            <xsl:variable name="Length" select="Length"/>
            <Section>
              <xsl:attribute name="SetNumber">
                <xsl:value-of select="$Points - $Polygons + 1"/>
              </xsl:attribute>
              <xsl:attribute name="SetID">
                <xsl:value-of select="$Points - $Polygons + 1"/>
              </xsl:attribute>
              <xsl:for-each select="../../../../../../../../Multi[@Code='Edge']/Section[1]/*">
                <xsl:copy>
                  <xsl:choose>
                    <xsl:when test="@Code='FromTo'">
                      <xsl:apply-templates select="@*"/>
                      <xsl:value-of select="$Points - $Polygons + 1"/>
                      <xsl:text>-</xsl:text>
                      <xsl:value-of select="$Points - $Polygons + 2"/>
                      <!--xsl:choose>
                        <xsl:when test="$Point = $CountPoint - 1">
                          <xsl:value-of select="$Points - $Polygons + 2 - $Point"/>
                        </xsl:when>
                        <xsl:otherwise>
                          <xsl:value-of select="$Points - $Polygons + 2"/>
                        </xsl:otherwise>
                      </xsl:choose-->
                      <xsl:if test="$Polygons > 1" >
                        <xsl:text> (</xsl:text>
                        <xsl:value-of select="$Polygons"/>
                        <xsl:text>)</xsl:text>
                      </xsl:if>
                    </xsl:when>
                    <xsl:when test="@Code='Length'">
                      <xsl:apply-templates select="@*"/>
                      <xsl:value-of select="$Length"/>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:apply-templates select="@* | node()"/>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:copy>
              </xsl:for-each>
            </Section>
          </xsl:for-each>
        </xsl:for-each>
      </xsl:for-each>
      <xsl:if test="count(../*/*/*/MIF/DATA/pline)=0">
        <Section>
          <xsl:attribute name="SetNumber">
            <xsl:value-of select="1"/>
          </xsl:attribute>
          <xsl:attribute name="SetID">
            <xsl:value-of select="1"/>
          </xsl:attribute>
          <xsl:for-each select="Section[1]/*">
            <xsl:copy>
              <xsl:choose>
                <xsl:when test="@Code='FromTo'">
                  <xsl:apply-templates select="@*"/>
                </xsl:when>
                <xsl:when test="@Code='Length'">
                  <xsl:apply-templates select="@*"/>
                </xsl:when>
                <xsl:when test="@Code='Definition'">
                  <xsl:apply-templates select="@*"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:apply-templates select="@* | node()"/>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:copy>
          </xsl:for-each>
        </Section>
      </xsl:if>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
